//
//  StudentCell.m
//  SDSY
//
//  Created by 蓝布鲁 on 2017/5/23.
//  Copyright © 2017年 蓝布鲁. All rights reserved.
//

#import "StudentCell.h"
#import "UserInfoModel.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "ViewEvalutionTeacherController.h"

typedef enum {
    StudentOrderStatusUnchecked = 1,
    StudentOrderStatusPassed = 2,
    StudentOrderStatusConfirmedArrive = 3,
    StudentOrderStatusCompletedEvaluate = 4
}StudentOrderStatus;

@interface StudentCell ()



@property (weak, nonatomic) IBOutlet UIImageView *headerImage;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *typeLabel;
@property (weak, nonatomic) IBOutlet UIButton *acceptBtn;            // 接受学生按钮 ---- 显示的状态为：1
@property (weak, nonatomic) IBOutlet UIButton *cancelBtn;            // 取消学生    ---- 显示的状态为：1
@property (weak, nonatomic) IBOutlet UIButton *confirmArrivedBtn;    // 确认到达按钮 ---- 显示的状态为：2
@property (weak, nonatomic) IBOutlet UIButton *noEvaluateBtn;        // 未评价按钮   ---- 显示的状态为: 3
@property (weak, nonatomic) IBOutlet UIButton *viewEvaluateBtn;      // 查看评价按钮 ---- 显示状态为：4


@end

@implementation StudentCell

-(void)setModel:(StudentModel *)model {
    _model = model;
    
    self.headerImage.layer.masksToBounds = true;
    self.headerImage.layer.cornerRadius = 25;
    
    [self.headerImage sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:[UIImage imageNamed:@"avatar_default_big"]];
    
    self.nameLabel.text = model.name;
    
    NSString *dateResult = [model.date formatDateWithInputFormat:@"yyyy-MM-dd" outputFormat:@"MM-dd"];
    
    NSString *startTime = [model.starttime formatDateWithInputFormat:@"hh:mm:ss" outputFormat:@"hh:mm"];
    
    NSString *endTime = [model.endtime formatDateWithInputFormat:@"hh:mm:ss" outputFormat:@"hh:mm"];
    
    self.timeLabel.text = [NSString stringWithFormat:@"%@ %@ ~ %@",dateResult,startTime,endTime];
    
    self.typeLabel.text = model.type;
    
    // 设置预约状态
    [self setStatus: (int)self.model.status];
}

// 设置学生预约状态
- (void)setStatus:(StudentOrderStatus)status {
    // 1.未审核 2.通过审核 3.确认学生到达 4.学生完成评价
    
    if (status == StudentOrderStatusUnchecked) {
        // 1. 未审核状态：需要导师接受或取消学生（接受预约）
        self.acceptBtn.hidden = false;
        self.cancelBtn.hidden = false;
        self.confirmArrivedBtn.hidden = true;
        self.noEvaluateBtn.hidden = true;
        self.viewEvaluateBtn.hidden = true;
    }else if (status == StudentOrderStatusPassed) {
        // 2. 通过审核状态：需要导师确认学生达到（确认到达）
        self.acceptBtn.hidden = true;
        self.cancelBtn.hidden = true;
        self.confirmArrivedBtn.hidden = false;
        self.noEvaluateBtn.hidden = true;
        self.viewEvaluateBtn.hidden = true;
    }else if (status == StudentOrderStatusConfirmedArrive) {
        // 3. 确认学生到达状态：学生已到达，等待学生评价（等待评价）
        self.acceptBtn.hidden = true;
        self.cancelBtn.hidden = true;
        self.confirmArrivedBtn.hidden = true;

        // 新需求，隐藏学生评价功能
        self.noEvaluateBtn.hidden = true;
        self.viewEvaluateBtn.hidden = true;
    }else if (status == StudentOrderStatusCompletedEvaluate) {
        // 4. 学生完成评价状态：学生已评价（查看评价）
        self.acceptBtn.hidden = true;
        self.cancelBtn.hidden = true;
        self.confirmArrivedBtn.hidden = true;
        self.noEvaluateBtn.hidden = true;

        // 新需求，隐藏学生评价功能
        self.viewEvaluateBtn.hidden = true;
    }
}

- (IBAction)acceptBtnTapped:(UIButton *)sender {
    [SVProgressHUD show];
    [[NetworkTool shareInstance] requireMethodType:POSTType
                                         URLString:@"SDSY/Treview"
                                        parameters:@{@"yid": self.model.yyid,@"sid": @(self.model.sid), @"status": @1}
                                           success:^(NSDictionary *respondDictionary) {
                                               [SVProgressHUD dismiss];
                                               [SVProgressHUD showSuccessWithStatus:@"接受成功"];
                                               
                                               // 重新设置预约状态
                                               [self setStatus: StudentOrderStatusPassed];
                                           } failure:^(NSError *error) {
                                               [SVProgressHUD dismiss];
                                               NSLog(@"%@",error);
                                               [SVProgressHUD showfailed];
                                           }];
}


- (IBAction)cancelBtnTapped:(id)sender {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入拒绝的原因" preferredStyle:UIAlertControllerStyleAlert];
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"请输入拒绝的原因";
    }];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [SVProgressHUD show];
        [[NetworkTool shareInstance] requireMethodType:POSTType
                                             URLString:@"SDSY/Treview"
                                            parameters:@{@"yid": self.model.yyid, @"sid": @(self.model.sid), @"status": @2, @"reson": alert.textFields.firstObject.text}
                                               success:^(NSDictionary *respondDictionary) {
                                                   
                                                   MyOrderedViewController *controller = (MyOrderedViewController *)self.viewController;
                                                   [controller.dataArray removeObjectAtIndex:self.indexPath.row];
                                                   
                                                   [controller.tableView beginUpdates];
                                                   [controller.tableView deleteRowsAtIndexPaths:@[self.indexPath] withRowAnimation:UITableViewRowAnimationLeft];
                                                   [controller.tableView endUpdates];
                                                   
                                                   [SVProgressHUD dismiss];
                                                   [SVProgressHUD showSuccessWithStatus:@"取消成功"];
                                               } failure:^(NSError *error) {
                                                   NSLog(@"%@",error);
                                                   [SVProgressHUD dismiss];
                                                   [SVProgressHUD showfailed];
                                               }];
    }]];
    [self.viewController presentViewController:alert animated:true completion:nil];
}


- (IBAction)confirmBtnTapped:(id)sender {
    [SVProgressHUD show];
    [[NetworkTool shareInstance] requireMethodType:POSTType
                                         URLString:@"SDSY/TcheckStudent"
                                        parameters:@{@"yid": self.model.yyid, @"sid": @(self.model.sid)} success:^(NSDictionary *respondDictionary) {
                                            [SVProgressHUD dismiss];
                                            [SVProgressHUD showSuccessWithStatus:@"接受成功"];
                                            
                                            // 重新设置预约状态
                                            [self setStatus: StudentOrderStatusConfirmedArrive];
                                        } failure:^(NSError *error) {
                                            NSLog(@"%@",error);
                                            [SVProgressHUD dismiss];
                                            [SVProgressHUD showfailed];
                                        }];
}

- (IBAction)ViewEvaluateBtnTapped:(id)sender {
    ViewEvalutionTeacherController *controller = [[ViewEvalutionTeacherController alloc] init];
    controller.star = self.model.star;
    controller.evalution = self.model.evalution;
    [self.viewController.navigationController pushViewController:controller animated:true];
}


@end
